<?php


if(file_exists(THINKROOT . '/uc_client/index.php')) {
	require_once(THINKROOT . '/uc_client/index.php');
}

function dss_uc_login($email, $pass) {
	if (!function_exists('uc_user_login')) return array();
	if (!defined('UC_API')) return array();
	$isuid = strpos($email, '@') ? 2 : 0;
	if (strtolower(UC_CHARSET)!='utf-8') {
		$email = mb_convert_encoding($email, UC_CHARSET, 'UTF-8');
	}
	//调用UC登录
	$ucresult = uc_user_login($email, $pass, $isuid, 0);
	$uckey = array( 'uid', 'username', 'password', 'email', 'duplicate' );
	$ucresult = array_combine($uckey, $ucresult);
	$ucresult = zuitu_encode_array($ucresult);
	$ucresult = zuitu_encode_detect($ucresult);
	global $db;
	$db->query('SET NAMES UTF8;');
	//如果UC登录成功
	if ($ucresult['uid']>0 && $ucresult['email'] ) {
		$eu = $db->once_fetch_assoc("select * from ".dbprefix."user_info where email='{$ucresult['email']}'");
		//如果本地也有此email的用户
		if ( $eu ) {
			//本地登录不成功而跳到UC登录的原因有可能是本地密码和UC不同步，所以这里有必要同步下UC和本地的用户密码
			$db->query("update ".dbprefix."user set pwd='".md5($ucresult['password'])."' where email='{$ucresult['email']}'");
			return $eu;
		}else{
			//UC有此用户，本地没有，创建一个
			$db->query("INSERT INTO ".dbprefix."user (`pwd` , `email`) VALUES ('".md5($pass)."', '$email');");
			$userid = $db->insert_id();
			//积分
			$db->query("insert into ".dbprefix."user_scores (`userid`,`scorename`,`score`,`addtime`) values ('".$userid."','注册','1000','".time()."')");
			//用户信息
			$arrData = array(
					'userid'			=> $userid,
					'fuserid'	=> 0,
					'username' 	=> $ucresult['username'],
					'email'		=> $email,
					'sex'	=> 0,
					'areaid'		=> 0,
					'ip'			=> getIp(),
					'count_score'	=> '1000',
					'addtime'	=> time(),
					'uptime'	=> time(),
			);
			$db->insertArr($arrData,dbprefix.'user_info');
			$userData = $db->once_fetch_assoc("select * from ".dbprefix."user_info where userid='$userid'");
			if($userData){
				//默认加入秀店
				$isgroup = $db->once_fetch_assoc("select optionvalue from ".dbprefix."user_options where optionname='isgroup'");
				if($isgroup['optionvalue'] != ''){
					$arrGroup = explode(',',$isgroup['optionvalue']);
					foreach($arrGroup as $item){
						$groupusernum = $db->once_num_rows("select * from ".dbprefix."group_users where `userid`='".$userid."' and `groupid`='".$item."'");
						if($groupusernum == '0'){
							$db->query("insert into ".dbprefix."group_users (`userid`,`groupid`,`addtime`) values('".$userid."','".$item."','".time()."')");
							//统计更新
							$count_user = $db->once_num_rows("select * from ".dbprefix."group_users where groupid='".$item."'");
							$db->query("update ".dbprefix."group set `count_user`='".$count_user."' where groupid='".$item."'");
						}
					}
				}
				return $userData;
			}
			return array();
		}

	}
	return array();
}

function dss_uc_register($email, $username, $password) {
	if (!function_exists('uc_user_login')) return true;
	if (!defined('UC_API')) return true;
	if (strtolower(UC_CHARSET)!='utf-8') {
		$username = mb_convert_encoding($username, UC_CHARSET, 'UTF-8');
	}
	$uid = uc_user_register($username, $password, $email);
	global $db;
	$db->query('SET NAMES UTF8;');
 	return $uid > 0;
}

function dss_uc_updatepw($email, $username, $password) {
	if (!function_exists('uc_user_login')) return true;
	if (!defined('UC_API')) return true;
	if (strtolower(UC_CHARSET)!='utf-8') {
		$username = mb_convert_encoding($username, UC_CHARSET, 'UTF-8');
		$email = mb_convert_encoding($email, UC_CHARSET, 'UTF-8');
	}
	$rid = uc_user_edit($username, $oldpw, $password, $email, 1);
	global $db;
	$db->query('SET NAMES UTF8;');
	return $rid >= 0;
}

function dss_uc_synlogin($email, $pass) {
	if (!function_exists('uc_user_login')) return array();
	if (!defined('UC_API')) return array();
	$isuid = strpos($email, '@') ? 2 : 0;
	if (strtolower(UC_CHARSET)!='utf-8') {
		$email = mb_convert_encoding($email, UC_CHARSET, 'UTF-8');
	}
	$ucresult = uc_user_login($email, $pass, $isuid, 0);
	$uckey = array( 'uid', 'username', 'password', 'email', 'duplicate' );
	$ucresult = array_combine($uckey, $ucresult);
	if ($ucresult['uid']>0 ) {
		$script_string = uc_user_synlogin($ucresult['uid']);
		$_SESSION['script_login']= $script_string;
	}
	global $db;
	$db->query('SET NAMES UTF8;');
}

function dss_uc_synlogout() {
	if (!function_exists('uc_user_login')) return true;
	if (!defined('UC_API')) return true;
	$script_string = uc_user_synlogout();
	$_SESSION['script_logout']= $script_string;
	global $db;
	$db->query('SET NAMES UTF8;');
}

function zuitu_encode_array($a=array()) {
	if (strtolower(UC_CHARSET)=='utf-8') return $a;
	foreach($a AS $k=>$o) {
		if(is_array($o)) $a[$k] = zuitu_encode_array($o);
		else $a[$k] = mb_convert_encoding($o, 'UTF-8', UC_CHARSET);
	}
	return $a;
}

function zuitu_encode_detect($a=array()) {
	if (strtolower(UC_CHARSET)=='utf-8') return $a;
	foreach($a AS $k=>$o) {
		if(is_array($o)) {
			$a[$k] = zuitu_encode_detect($o);
		}
		else {
			$en = mb_detect_encoding($o);
			if ( strtoupper($en)!='UTF-8' ) {
				$a[$k] = mb_convert_encoding($o, 'UTF-8', $en);
			}
		}
	}
	return $a;
}